package com.smasher.update.dialog

import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.smasher.update.R
import com.smasher.update.databinding.FragmentDialogUpdateBinding

class UpdateVersionDialog : BaseDialog(), View.OnClickListener {

    private lateinit var mBinding: FragmentDialogUpdateBinding

    private var onUpdateListener: OnUpdateListener? = null

    private var title: String? = null
    private var content: String? = null
    private var description: String? = null
    private var url: String? = null
    private var versionCode: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.UpdateDialogStyle)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentDialogUpdateBinding.inflate(
            LayoutInflater.from(context), container, false
        )
        return mBinding.root
    }

    override fun initData() {

    }

    override fun initView() {

        if (TextUtils.isEmpty(description)) {
            mBinding.description.visibility = View.GONE
        } else {
            mBinding.description.text = description
        }

        mBinding.title.text = title
        mBinding.content.text = content

        mBinding.cancel.setText(R.string.app_next_time)
        mBinding.download.setText(R.string.app_download)

        mBinding.cancel.setOnClickListener(this)
        mBinding.download.setOnClickListener(this)
        mBinding.versionIgnore.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                onUpdateListener?.ignoreVersion(parseVersion())
            } else {
                onUpdateListener?.ignoreVersion(0L)
            }
        }
    }

    private fun parseVersion(): Long {
        return try {
            versionCode?.toLong() ?: 0L
        } catch (e: Exception) {
            0L
        }
    }

    override fun getWidth(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun getHeight(): Int {
        return ViewGroup.LayoutParams.WRAP_CONTENT
    }

    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    public fun initTitleContent(
        title: String,
        content: String,
        description: String,
        url: String,
        code: String
    ) {
        if (!TextUtils.isEmpty(title)) {
            this.title = title
        }
        if (!TextUtils.isEmpty(content)) {
            this.content = content
        }
        if (!TextUtils.isEmpty(description)) {
            this.description = description
        }
        if (!TextUtils.isEmpty(url)) {
            this.url = url
        }
        if (!TextUtils.isEmpty(code)) {
            this.versionCode = code
        }
    }

    fun setUpdateListener(onUpdateListener: OnUpdateListener) {
        this.onUpdateListener = onUpdateListener
    }

    interface OnUpdateListener {

        fun gotoDownload(url: String)

        fun nextTime()

        fun ignoreVersion(versionCode: Long)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.download -> {
                if (onUpdateListener != null && !TextUtils.isEmpty(url)) {
                    onUpdateListener!!.gotoDownload(this.url!!)
                    dismiss()
                }
            }

            R.id.cancel -> {
                if (onUpdateListener != null) {
                    onUpdateListener!!.nextTime()
                    dismiss()
                }
            }

            else -> {}
        }
    }

    companion object {

        /**
         *
         */
        @JvmStatic
        fun newInstance(): UpdateVersionDialog {
            return UpdateVersionDialog().apply {
                arguments = Bundle()
            }
        }
    }
}